package com.tedu.T2;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f=new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setUsername("admin");
        f.setPassword("admin");

        Channel c=f.newConnection().createChannel();

        c.queueDeclare("task_queue", false, false, false, null);

        DeliverCallback deliverCallback=new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] bytes=delivery.getBody();
                String msg=new String(bytes);
                System.out.println("hao"+msg);
                for (int i=0;i<msg.length();i++){
                    if (msg.charAt(i)=='.'){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
                /*手动发送回执
                * @Param
                * 1.回执数据
                * 2.是否同时确认多条消息*/
                c.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

                System.out.println("-------------Already-------------");

            }
        };
        CancelCallback cancelCallback=new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };
//        每次只抓取一条，在处理完后都不再抓取
        c.basicQos(1);
        /*@Param
        * 消费数据
        * ack模式
        * true自动确认
        * FALSE 手动确认*/

        c.basicConsume("task_queue", false,deliverCallback,cancelCallback);

    }


}
